package com.wnhz.work.jy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wnhz.work.jy.R;
import com.wnhz.work.jy.bean.AssetsBean;

import java.util.List;

/**
 * Created by d on 2016/10/9.
 */
public class AssetsAdapter extends BaseAdapter {
    private Context context;
    private List<AssetsBean.MyGoods> mList;
    private View.OnClickListener listener;

    public AssetsAdapter(Context context, List<AssetsBean.MyGoods> mList, View.OnClickListener listener) {
        this.context = context;
        this.mList = mList;
        this.listener = listener;
    }



    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        MyHolder holder;
        if (view == null){
            holder = new MyHolder();
            view = LayoutInflater.from(context).inflate(R.layout.item_assets,null);
            holder.name = (TextView) view.findViewById(R.id.name);
            holder.number = (TextView) view.findViewById(R.id.number);
            holder.prime_cost = (TextView) view.findViewById(R.id.prime_cost);
            holder.change_the_price = (TextView) view.findViewById(R.id.change_the_price);
            holder.btn = view.findViewById(R.id.btn);

            view.setTag(holder);
        }

        holder = (MyHolder) view.getTag();
        holder.btn.setOnClickListener(listener);
        holder.btn.setTag(i);
        holder.name.setText(mList.get(i).getGoodName());
        holder.number.setText(mList.get(i).getShowNumber());
        holder.prime_cost.setText("¥ "+mList.get(i).getGoodPrice());


        if ("0".equals(mList.get(i).getIsAppreciation())){
            holder.change_the_price.setText("+¥ "+mList.get(i).getVariation());
            holder.change_the_price.setTextColor(0xffe2bb0a);
        }else {
            StringBuilder price = new StringBuilder(mList.get(i).getVariation());
            if (price.indexOf("-")>=0){
                price.insert(price.indexOf("-")+1,"¥ ");
            }else {
                price.insert(0,"-¥ ");
            }
            holder.change_the_price.setText(price);
            holder.change_the_price.setTextColor(0xffff3c3d);
        }
        if ("1".equals(mList.get(i).getBuy_back())){
            holder.btn.setBackgroundResource(R.drawable.btn_shape);
        }else {
            holder.btn.setBackgroundResource(R.drawable.btn_shape_nor);
        }

        return view;
    }

    private class MyHolder{
        TextView name;
        TextView number;
        TextView prime_cost;
        TextView change_the_price;//张为主色 跌为红色
        View btn;

    }
}

